﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Production;
using BusinessObjects.Production;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer.Production
{
    public class SqlServerStockDao : IStockDao
    {
        public List<Stock> GetStocks()
        {
            string procedures = "Get_All_Stocks";

            return Db.ReadList(procedures, true, Make, null);
        }

        public List<Stock> GetStocksByBranch(string restaurantId)
        {
            string procedures = "Get_All_Stocks_ByBranch";

            object[] parms = { "@RestaurantID", restaurantId };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public Stock GetStock(string stockId)
        {
            string procedures = "Get_Stock_ById";

            object[] parms = { "@StockID", stockId };
            return Db.Read(procedures, true, Make, parms);
        }

        public int GetTotalPage()
        {
            string procedures = "Get_PageSize_Stock";

            return Db.ReadTotalPage(procedures, true, null);
        }

        public int GetTotalPageByBranch(string restaurantId)
        {
            string procedures = "Get_PageSize_Stock_ByBrach";

            object[] parms = { "@RestaurantID", restaurantId };
            return Db.ReadTotalPage(procedures, true, parms);
        }

        public List<Stock> GetStocksByPage(int currentPage)
        {
            string procedures = "Get_Paged_Stocks";

            object[] parms = {  "@WhereClause", null, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<Stock> GetStocksByPageAndBranch(int currentPage, string restaurantId)
        {
            string procedures = "Get_Paged_Stocks_ByBranch";

            object[] parms = {  "@WhereClause", null, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage,
                                "@RestaurantID", restaurantId};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<Stock> GetStocksByIsActive(bool isActive)
        {
            string procedures = "Get_All_Stocks_ByIsActive";

            object[] parms = { "@IsActive", isActive };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<Stock> GetStocksByIsActiveAndBranch(bool isActive, string restaurantId)
        {
            string procedures = "Get_All_Stocks_ByWhereClause";

            object[] parms = { "@WhereClause", " RestaurantID = '" + restaurantId + "'" };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public void InsertStock(Stock stock)
        {
            string sql = "Insert_Stock";

            stock.StockID = Db.Insert(sql, true, Take(stock));
        }

        public void UpdateStock(Stock stock)
        {
            string sql = "Update_Stock";

            Db.Update(sql, true, Take(stock));
        }

        public void DeleteStock(Stock stock)
        {
            string sql = "Delete_Stock";

            Db.Update(sql, true, Take(stock.StockID));
        }

        private object[] Take(string stockId)
        {
            return new object[]  
            {
                "@StockId", stockId,
            };
        }

        private object[] Take(Stock stock)
        {
            return new object[]  
            {
                "@StockID", stock.StockID,
                "@StockCode", stock.StockCode,
                "@StockName", stock.StockName,
                "@RestaurantID", stock.RestaurantID,
                "@Description", stock.Description,
                "@IsActive", stock.IsActive
            };
        }

        private static Func<IDataReader, Stock> Make = reader =>
            new Stock
            {
                StockID = reader["StockID"].AsString(),
                StockCode = reader["StockCode"].AsString(),
                StockName = reader["StockName"].AsString(),
                RestaurantID = reader["RestaurantID"].AsString(),
                Description = reader["Description"].AsString(),
                IsActive = reader["IsActive"].AsBool()
            };
    }
}
